package com.xhwl.data.listenter;

import java.util.Date;
import com.xhwl.common.enums.config.BusinessType;
import com.xhwl.common.enums.config.InitializeRoleEnum;
import com.xhwl.common.enums.config.RoleTypeEnum;
import com.xhwl.common.pojo.cent.sdata.Role;
import com.xhwl.common.query.cent.sdata.RoleQuery;
import com.xhwl.data.service.IRoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author JCccc
 * @Description 内置角色初始化 监听器
 * @Date 2021/7/5 9:27
 */
@Component
public class InitializeRoleListener implements ApplicationRunner {
    private static final Logger log = LoggerFactory.getLogger(InitializeRoleListener.class);
    @Autowired
    IRoleService roleService;
    @Override
    public void run(ApplicationArguments args) {
        try {
            log.info("准备开始初始化内置角色......");
            List<Role> needInitializeRoles = new ArrayList<>();
            for (InitializeRoleEnum initializeRoleEnum : InitializeRoleEnum.values()) {
                Role role = new Role();
                role.setId(initializeRoleEnum.getId());
                role.setIndustryId(initializeRoleEnum.getIndustryId());
                role.setName(initializeRoleEnum.getName());
                role.setCode(initializeRoleEnum.getCode());
                role.setGrade(BusinessType.INDUSTRY.getCode());
                role.setType(RoleTypeEnum.COMMON.getCode());
                role.setIsInitialized(true);
                role.setStatus((short) 1);
                role.setIsDeleted(false);
                role.setUpdatedAt(new Date());
                role.setCreatedAt(new Date());
                needInitializeRoles.add(role);
            }
            needInitializeRoles = filterExistRoles(needInitializeRoles);
            if (!CollectionUtils.isEmpty(needInitializeRoles)) {
                roleService.initializeRoleBatch(needInitializeRoles);
            }
            log.info("初始化内置角色结束......");
        } catch (Exception e) {
            e.printStackTrace();
            log.info("初始化内置角色异常={}", e.getMessage());
        }
    }

    /**
     * 过滤掉数据库内已经存在的角色数据，剩下的就是初始化需要新增的
     *
     * @param needInitializeRoles
     * @return
     */
    public List<Role> filterExistRoles(List<Role> needInitializeRoles) {
        List<Integer> roleIds = needInitializeRoles.stream().map(Role::getId).collect(Collectors.toList());
        RoleQuery roleQuery = new RoleQuery();
        roleQuery.setIdList(roleIds);
        roleQuery.setNeedCommonInitialized(true);
        List<Role> roleFromDb = roleService.getRoleList(null, roleQuery);
        List<Integer> roleFromDbIds = roleFromDb.stream().map(Role::getId).collect(Collectors.toList());
        List<Role> insertResultRoles = new ArrayList<>();
        for (Role role : needInitializeRoles) {
            if (!roleFromDbIds.contains(role.getId())) {
                insertResultRoles.add(role);
            }
        }
        return insertResultRoles;
    }

}
